package solution._0020._0020;

import java.util.Stack;

/**
 * Created by chen_snow
 * Date: 2021/5/13 23:33
 * Description: 有效的括号
 */
public class Solution {
    public boolean isValid(String s) {
        Stack<Character> data = new Stack<>();

        for (int i = 0; i < s.length(); i++) {
            if (isRightQuote(s.charAt(i))) {
                if (!checkValid(data, s.charAt(i))) {
                    return false;
                }
            } else {
                data.push(s.charAt(i));
            }
        }
        return data.isEmpty();
    }

    public boolean isRightQuote(Character character) {
        return character == ')' || character == '}' || character == ']';
    }

    public boolean checkValid(Stack<Character> data, Character character) {
        if (data.isEmpty()) {
            return false;
        }

        Character ch = data.pop();

        switch (character) {
            case ')':
                return ch == '(';
            case '}':
                return ch == '{';
            case ']':
                return ch == '[';
            default:
                return false;
        }
    }
}
